package com.whstudio.article;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.whstudio.util.mvc.dao.AbstractDAO;
import com.whstudio.util.xml.IDocument;

/**
 * 求助帖 article
 * 
 * @author wbxuhui
 * 
 */
public class ArticleDAO extends AbstractDAO {

    /**
     * 保存求助帖信息
     * 
     * @param article
     * @return
     */
    public int doSaveArticle(ArticleVO article) throws SQLException {
        String sql = "insert into article(user_id,user_name,article_subject,article_content)"
                + " values(?,?,?,?) ";

        Connection conn = getConnection();
        PreparedStatement ps = null;
        int result = -1;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, article.getUserId());
            ps.setString(2, article.getUserName());
            ps.setString(3, article.getArticleSubject());
            ps.setString(4, article.getArticleContent());
            
            result = ps.executeUpdate();
        } finally {
            endConnection(null, ps, conn);
        }
        return result;
    }

    /**
     * 注册用户
     * 
     * @param user
     * @return
     */
    public int registerUser(UserVO user) throws SQLException {
        String sql = "insert into user(user_name,department,address,email) "
                + " values(?,?,?,?)";
        Connection conn = getConnection();
        PreparedStatement ps = null;
        int result = -1;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getDepartment());
            ps.setString(3, user.getAddress());
            ps.setString(4, user.getEmail());
            
            result = ps.executeUpdate();
        } finally {
            endConnection(null, ps, conn);
        }
        return result;
    }

    /**
     * 查询所有的求助帖
     * @return
     * @throws SQLException
     */
    public IDocument doQueryArticles() throws SQLException{
        String sqlString="select * from article";
        return getResultDocumentBySql(sqlString);
    }
}
